import java.util.*;

public class AdjMatrixGraph {
	public int V;
    public int[] mVexs=null;
    public int[][] edge=null;
    boolean[][] visited=null;
	public int[][] getEdge() {
		return edge;
	}
	public void setEdge(int[][] edge) {
		this.edge = edge;
	}
	public AdjMatrixGraph(int V,int[] mVexs, int[][] edge) {
		super();
		this.V=V;
		this.mVexs = mVexs;
		this.edge = edge;
		this.visited=new boolean[V][V];
		for(int i=0;i<V;i++)
			for(int j=0;j<V;j++)
				visited[i][j]=false;
	    
	}
	

    /*
     * 返回顶点v的第一个邻接顶点的索引，失败则返回-1
     */
    private int firstVertex(int v) {

        if (v<0 || v>(mVexs.length-1))
            return -1;

        for (int i = 0; i < mVexs.length; i++)
            if (edge[v][i] == 1)
                return i;
        return -1;
    }

    /*
     * 返回顶点v相对于w的下一个邻接顶点的索引，失败则返回-1
     */
    private int nextVertex(int v, int w) {

        if (v<0 || v>(mVexs.length-1) || w<0 || w>(mVexs.length-1))
            return -1;

        for (int i = w + 1; i < mVexs.length; i++)
            if (edge[v][i] == 1)
                return i;
        return -1;
    }
    
    public boolean searchRoute(int u,int v){
    	 for (int w = firstVertex(u); w >= 0; w=nextVertex(u, w)) {
    		System.out.println("w:"+w);
 	    	System.out.println("v:"+v);
    		 if(this.visited[u][w]) continue;
    		 else this.visited[u][w]=true;
             if(w==v){  
            	 System.out.println("w==v");
            	 return true;
            	 }
             else
            	searchRoute(w,v);          
         }
    	 return false;
    }
    
    public boolean judgeSemiconnected(){
    	System.out.println("judgeSemiconnected");
    	for(int i=0;i<edge.length;i++)
    		for(int j=0;j<edge.length;j++){
    			//System.out.println("i:"+i);
    	    	//System.out.println("j:"+j);
    			if(!searchRoute(i,j)){
    				System.out.println(i+"-->"+j+"  Disconnected");
    				return false;
    			}else{
    				System.out.println(i+"-->"+j+"  Connected");
    			}
    		}
    		
    	return true;
    	
    }
    
    public static void main(String[] args) {
    	int[] mVexs={0,1,2,3,4,5};
		int[][] edge={{1,1,0,0,0,0},{0,0,0,1,0,0},{0,0,0,0,0,1},{0,0,1,0,0,0},{0,0,0,1,0,0},{0,0,0,0,1,0}};
		AdjMatrixGraph g=new AdjMatrixGraph(5,mVexs,edge);
		//System.out.println(g.judgeSemiconnected());
		System.out.println(g.searchRoute(0, 1));
		
	}
    
}